<?php
/**
 * Description of SoBizCommon
 *
 * @author Alen
 * @since 1.0.0 2013-10-2 sutroon
 */
class SoBizCommon {
    
    private $dm;
    private $msg;
    private $logininfo;
    
    public function __construct($auto_connect=true) {
        $this->dm=new SoDataModule($auto_connect);
        $this->msg=array();
    }
    
    
    
    /******************************************************************************************
     * 业务方法(BTM Begin)
     * 
     * 包含业务逻辑的方法
     *******************************************************************************************/
    
    /**
     * 获取字典表的值
     * @param type $name
     * @return type
     */
    public function data_get_value_by_name($name){
        return $this->dm->select_value('tgeneraldata', sprintf('Name=\'%s\'',$query),'Value');
    }
    
	
	/**
	* 2013-5-6
	*/
	function get_pm_count($uname,$state=0){
		global $db;
		return $db->fetch_value("select count(0) from tuserpersonalmessage where ToUserName='$uname' and State=$state");
	}
	
	function get_generalvalue($name){
		global $db;
		return htmlspecialchars_decode($db->fetch_value("select `Value` from tgeneraldata where `Name`='$name'"));
	}

	function update_generalvalue($name,$value,$tag){
		global $db;
		if($db->fetch_value("select count(0) from tgeneraldata where `Name`='$name'")==0){
			$db->execsql("insert into tgeneraldata (`Name`, `Value`, `ExTags`) values ('$name','$value','$tag')");	
		}else{
			$db->execsql("update tgeneraldata set `Value`='$value',  ExTags='$tag' where `Name`='$name';");
		}
	}
        
        /**
         * 
         * @global type $db
         * @param type $name
         * @param type $tmp %s=URL,%s=Picture,%s=Title
         * @return type
         */
	function get_ads_content($name,$tmp='<a href="%s"><img src="%s" alt="%s" /></a>'){
		global $db;
		$rs=$db->fetch_all("select a.Url, a.Picture, a.Title from tgeneraladvertisement a left join tgeneraladvertisement b on a.ParentID=b.ID where b.Title='$name' order by a.Ordinal, a.CreatedTime desc");
		$return='';
		if($rs){
			foreach($rs as $row){
				$return.=sprintf($tmp,$row['Url'],$row['Picture'],$row['Title']);	
			}
		}
		return $return;
	}
	
	function get_generaltree($val,$table,$tag='',$fields='ID,Name'){
		if(!$fields) $fields='ID, Name';
		global $db;
		$sqltag=$tag?" and ExTags='$tag'":'';
		$rs=mysql_query("select $fields from $table where ParentID=0 $sqltag");
		$str='<option value=\'0\'>请选择</option>';$chk='';
		while($row=mysql_fetch_array($rs)){
			$chk='';
			if($val==$row[0]) $chk=' selected=\'selected\'';
			$str=$str. '<option value=\''.$row[0].'\''.$chk.'>'.$row[1].'</option>';
			$rs2=mysql_query("select ID, Name from $table where ParentID=".$row[0]);
			if(!$rs2) continue;
			while($row2=mysql_fetch_array($rs2)){
				$chk='';
				if($val==$row2[0]) $chk=' selected=\'selected\'';
				$str=$str. '<option value=\''.$row2[0].'\''.$chk.'>--'.$row2[1].'</option>';			
			}
		}
		return $str;
	}
	
	
	/**
	* 获取订单数量 2013-5-6 by sutroon
	*/
	function get_order_count($uname,$state=0,$isbuyer=true){
		global $db;
		$sqlc=' where ';
		if($uname) $sqlc.=($isbuyer?'BuyerName':'SellerName').'=\''.$uname.'\' and ';
		$sqlc.=' State='.$state;
		return $db->fetch_value('select count(0) from torderorder '.$sqlc);
	}
	
	/**
	* 获取商品数量 2013-5-6 by sutroon
	*/
	function get_product_count($uname,$state=-1){
		global $db;
		$sqlstate = $state==-1?'':" and State=$state ";
		return $db->fetch_value("select count(0) from tproductgoods where UserName='$uname' $sqlstate");
	}
	
	/**
	* 获取购物车总价格 2013-5-14 by sutroon
	*/
	function get_shoppingcar_amount($arr){
		if(!is_array($arr)) return 0;
		global $db;
		$rs=$db->fetch_all('select ID,Price from tproductgoods where ID in ('.implode(',',array_keys($arr)).');');
		if(!$rs) return 0;
		$amount=0;
		foreach($rs as $row){
			$amount+=$row[1]*$arr[$row[0]];
		}
		return $amount;
	}
        
        /********************************************************************业务方法(BTM End)###*/
        
    
    
}

?>
